<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        * {
            margin: 0;
            padding: 0;
        }
        
        body {
            width: 300px;
            margin: 0 auto;
        }
        
        table,
        tr,
        td,
        th {
            border: 1px solid #333333;
            border-collapse: collapse;
            padding: 10px;
        }
        
        form {
            width: 324px;
            border: 1px solid #333333;
            margin-top: 20px;
            padding-left: 20px;
        }
        
        span {
            display: inline-block;
            width: 50px;
            /* border: 1px solid #333333; */
            vertical-align: middle;
            margin: 5px;
            text-align: center;
        }
        
        form button {
            margin-left: 250px;
            margin-bottom: 10px;
        }
    </style>
</head>

<body>
    <table id="table">
        <tr>
            <th>name</th>
            <th>email</th>
            <th>salary</th>
            <th>option</th>
        </tr>
        <tr>
            <td>tom</td>
            <td>tom@163.com</td>
            <td>10000</td>
            <td><a href="delete?id=001">delete</a></td>
        </tr>
        <tr>
            <td>amy</td>
            <td>amy@163.com</td>
            <td>10000</td>
            <td><a href="delete?id=002">delete</a></td>
        </tr>
        <tr>
            <td>jack</td>
            <td>jack@163.com</td>
            <td>10000</td>
            <td><a href="delete?id=003">delete</a></td>
        </tr>
    </table>
    <form action="">
        <h3>添加新员工</h3>
        <span>name：</span><input type="text" id="name"> <br>
        <span>email：</span><input type="text" id="email"> <br>
        <span>salary：</span><input type="text" id="salary"> <br>
        <button id="btn" type="button">添加</button>
    </form>
</body>
<script>
    var index = 4;
    addAEvent();
    var btn = document.getElementById('btn');
    btn.onclick = function() {
        var name = document.getElementById('name').value;
        var email = document.getElementById('email').value;
        var salary = document.getElementById('salary').value;
        var table = document.getElementById('table');
        // 方法1：会影响所有的table中的元素
        // table.innerHTML +=
        //     "<td>" + name + "</td>" +
        //     "<td>" + email + "</td>" +
        //     "<td>" + salary + "</td>" +
        //     "<td><a href='#' id='delete'>delete</a></td>"
        // 方法2：
        var tr = document.createElement('tr');
        var nameTd = document.createElement('td');
        nameTd.innerHTML = name;
        var emailTd = document.createElement('td');
        emailTd.innerHTML = email;
        var salaryTd = document.createElement('td');
        salaryTd.innerHTML = salary;
        var td = document.createElement("td");
        td.innerHTML = "<td><a href='delete?id=00" + index + "'>delete</a></td>";
        tr.appendChild(nameTd);
        tr.appendChild(emailTd);
        tr.appendChild(salaryTd);
        tr.appendChild(td);
        var tbody = table.getElementsByTagName('tbody')[0];
        tbody.appendChild(tr);
        index++;
        addAEvent();
    }

    function addAEvent() {
        var allA = document.getElementsByTagName('a');
        for (var i = 0; i < allA.length; i++) {
            allA[i].onclick = function() {
                // 响应函数return false取消跳转的默认行为
                // 点击哪个哪个就是this
                var tr = this.parentNode.parentNode;
                var flag = confirm("确认删除" + tr.children[0].innerHTML + "吗？");
                if (flag) {
                    tr.parentNode.removeChild(tr);
                    index--;
                }
                return false;
            }
        };
    }
</script>

</html>